package com.kang.gaklibrary.service.impl;

import com.kang.gaklibrary.mapper.BookMapper;
import com.kang.gaklibrary.mapper.UserMapper;
import com.kang.gaklibrary.model.User;
import com.kang.gaklibrary.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    BookMapper bookMapper;

    // 查找到数据库相同userId的user对象返回
    @Override
    public User userLogin(String userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        return user;
    }

    // 将user对象的数据插入到数据库
    @Override
    public Integer insertUser(User user) {
        return userMapper.insertSelective(user);
    }

    // 更新数据
    @Override
    public int updateSelectiveByKey(String userId, String key, String value) {
        User user = new User();
        user.setUserId(userId);
        if ("userName".equals(key)){
            user.setUserName(value);
        }else if ("userSex".equals(key)){
            if (value.equals("男")){
                user.setUserSex(0);
            }else {
                user.setUserSex(1);
            }
        }else if ("userAge".equals(key)){
            Integer age = Integer.valueOf(value);
            user.setUserAge(age);
        }else if ("userQq".equals(key)){
            user.setUserQq(value);
        }else if ("userLocation".equals(key)){
            user.setUserLocation(value);
        }else if ("userDate".equals(key)){
            Date date = Date.valueOf(value);
            user.setUserDate(date);
        }else if ("userPwd".equals(key)) {
            user.setUserPwd(value);
        }else {
            System.out.println("。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。");
        }

        return userMapper.updateByPrimaryKeySelective(user);
    }
}
